var api = require('../config/api.js');


function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 封封微信的的request
 */
// function request(url, data = {}, method = "GET") {
//   return new Promise(function (resolve, reject) {
//     data.dian_token = uni.getStorageSync(api.config.DIAN_TOKEN);//每次请求组装上token
//     data.xcxflag = api.config.XCX_FLAG;
//     data.belong = api.config.XCX_BELONG;
//     data.openid = uni.getStorageSync('openid');
//     console.log('belong:' + api.config.XCX_BELONG)
//     console.log('openid:' + uni.getStorageSync('openid'))
//     uni.request({
//       url: url,
//       data: data,
//       method: method,
//       header: {
//         'Content-Type': 'application/json',
//         'X-Nideshop-Token': uni.getStorageSync('DIAN_TOKEN')
//       },
//       success: function (res) {
//         console.log("success");

//         if (res.statusCode == 200) {

//           if (res.data.errno == 401) {
//             //需要登录后才可以操作

//             let code = null;
//             return login().then((res) => {
//               code = res.code;
//               return getUserInfo();
//             }).then((userInfo) => {
//               //登录远程服务器
//               request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => {
//                 if (res.errno === 0) {
//                   //存储用户信息
//                   uni.setStorageSync('userInfo', res.data.userInfo);
//                   uni.setStorageSync('DIAN_TOKEN', res.data.token);
//                   uni.setStorageSync('parentOpenid', res.data.userInfo.parentopenid);

                  
//                   resolve(res);
//                 } else {
//                   reject(res);
//                 }
//               }).catch((err) => {
//                 reject(err);
//               });
//             }).catch((err) => {
//               reject(err);
//             })
//           } else {
//             resolve(res.data);
//           }
//         } else {
//           reject(res.errMsg);
//         }

//       },
//       fail: function (err) {
//         reject(err)
//         console.log("failed")
//       }
//     })
//   });
// }

/**
 * 检查微信会话是否过期
 */
function checkSession() {
  return new Promise(function (resolve, reject) {
    uni.checkSession({
      success: function () {
        resolve(true);
      },
      fail: function () {
        reject(false);
      }
    })
  });
}

/**
 * 调用微信登录
 */
function login() {
  return new Promise(function (resolve, reject) {
    uni.login({
      success: function (res) {
        if (res.code) {
          //登录远程服务器
          console.log(res)
          resolve(res);
        } else {
          reject(res);
        }
      },
      fail: function (err) {
        reject(err);
      }
    });
  });
}

function getUserInfo() {
  return new Promise(function (resolve, reject) {
    uni.getUserInfo({
      withCredentials: true,
      success: function (res) {
        console.log("getUserInfo success...")
        console.log(res)
        resolve(res);
      },
      fail: function (err) {
        console.log("getUserInfo fail...")
        reject(err);
      }
    })
  });
}

function redirect(url) {

  //判断页面是否需要登录
  if (false) {
    uni.redirectTo({
      url: '/pages/auth/login/login'
    });
    return false;
  } else {
    uni.redirectTo({
      url: url
    });
  }
}

function showErrorToast(msg) {
  uni.showToast({
    title: msg,
    image: '/static/images/icon_error.png'
  })
}

module.exports = {
  formatTime,
  redirect,
  showErrorToast,
  checkSession,
  login,
  getUserInfo,
}


